using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
  public class DateRange : IEnumerable<DateTime>
  {
    #region Fields
    private DateTime _startDate;
    private DateTime _endDate;
    #endregion
    #region Properties

    public DateTime StartDate
    {
      get { return _startDate; }
      set { _startDate = value; }
    }

    public DateTime EndDate
    {
      get { return _endDate; }
      set { _endDate = value; }
    }

    public TimeSpan Span
    {
      get { return _endDate - _startDate; }
    }
    #endregion
    #region Static Methods
    public static DateRange Parse(string date)
    {
      return Parse(date, date);
    }

    public static DateRange Parse(string startDate, string endDate)
    {
      return new DateRange(DateTime.Parse(startDate), DateTime.Parse(endDate));
    }
    #endregion

    #region Constructors
    public DateRange(DateTime date) : this(date,date)
    {

    }

    public DateRange(DateTime startDate, DateTime endDate)
    {
      if (_startDate > _endDate)
        throw new ArgumentOutOfRangeException("StartDate cannot occur after the EndDate");

      _startDate = startDate;
      _endDate = endDate;
    }
    #endregion
    #region Public Methods
    public TimeRange ToTimeRange()
    {
      return new TimeRange(_startDate.TimeOfDay, _endDate.TimeOfDay);
    }
    #endregion
    #region IEnumerable<DateTime> Members
    public IEnumerator<DateTime> GetEnumerator()
    {
      for (DateTime day = _startDate; day < _endDate; day = day.AddDays(1))
        yield return day;
    }
    #endregion
    #region IEnumerable Members
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
      for (DateTime day = _startDate; day < _endDate; day = day.AddDays(1))
        yield return day;
    }
    #endregion
  }
}
